//
//  ZBYTabBar.swift
//  ZBYBlog
//
//  Created by zby on 16/2/24.
//  Copyright © 2016年 zby. All rights reserved.
//
/*
[<_UITabBarBackgroundView: 0x7ffaf94a7fd0; frame = (0 0; 375 49); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7ffaf9405090>>,
<UIButton: 0x7ffaf94e8230; frame = (150 0; 75 49); opaque = NO; layer = <CALayer: 0x7ffaf94e80e0>>,
<UITabBarButton: 0x7ffafb9254d0; frame = (2 1; 90 48); opaque = NO; layer = <CALayer: 0x7ffaf95b48e0>>,
<UITabBarButton: 0x7ffafb8bd110; frame = (96 1; 90 48); opaque = NO; layer = <CALayer: 0x7ffaf9404aa0>>, 
<UITabBarButton: 0x7ffafb8be460; frame = (190 1; 89 48); opaque = NO; layer = <CALayer: 0x7ffafb8c03b0>>,
<UITabBarButton: 0x7ffafb8c2ba0; frame = (283 1; 90 48); opaque = NO; layer = <CALayer: 0x7ffafb8c3120>>,
<UIImageView: 0x7ffaf94d3c20; frame = (0 -0.5; 375 0.5); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7ffaf9485420>>]===
*/
import UIKit

class ZBYTabBar: UITabBar {

   //自定义视图
    
    //第一步：重写init
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //第二步：创建一个私有方法，来添加视图和设置约束
    func setUpUI(){
        //添加一个按钮
        addSubview(composeButton)
    }
    
    //第三步：懒加载
    lazy var composeButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        //设置按钮的属性
        
        //背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //添加点击事件
        
        
        //设置大小
        button.sizeToFit()

        return button
        
    }()
    
    //布局子控件
    override func layoutSubviews() {
        super.layoutSubviews()

//    print("\(subviews)===")
        //创建一个索引
        var index = 0
        //设置frame
        let width = bounds.size.width/5
        let hight = bounds.size.height
        
        for subview in subviews{
            
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
                subview.frame = CGRectMake(width * CGFloat(index++), 0, width, hight)
                
                //让加号按钮放在中间
                if index == 2 {
                    index++
                    //设置按钮的位置
                    composeButton.frame = CGRectMake(2 * width, 0, width, hight)

                }
            }
            
                   }
    
    
    }
    
    
}









